﻿// <copyright file="BlockElement.cs" company="Marcin Wosik">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Marcin Wosik</author>
// <email>mawo11@gmail.com</email>
// <date>2010-02-25</date>
// <summary></summary>
namespace GameOfLife.GameData.Blocks
{
    using System.Windows;
    using GameOfLife.GameData;

    /// <summary>
    /// Block element 
    /// </summary>
    public class BlockElement : ElementBase
    {
        #region Methods

        /// <summary>
        /// Check whether the point is the element rectangle 
        /// </summary>
        /// <param name="pt">Instancje of point class</param>
        /// <returns>True - if is in, False - if not </returns>
        public override bool CheckCord(Point pt)
        {
            if (pt != null && this.Drawer != null && this.Drawer.Points.Count == 4)
            {
                return pt.X >= this.Drawer.Points[0].X && pt.X <= this.Drawer.Points[1].X && pt.Y >= this.Drawer.Points[0].Y && pt.Y <= this.Drawer.Points[3].Y;
            }

            return false;
        }

        /// <summary>
        /// Making conenction with other elements
        /// </summary>
        /// <param name="data">Game settings</param>
        public override void MakeConnection(GameSettings data)
        {
            base.MakeConnection(data);

            int minPosX = PosX - 1;
            int minPosY = PosY - 1;
            int maxPosX = PosX + 1;
            int maxPosY = PosY + 1;

            // left side
            if (minPosX >= 0)
            {
                // left top
                if (minPosY >= 0 && data.ConnectionType == ConnectionType.Moore)
                {
                    this.ConnectedElements.Add(GetFromCord(data.Elements, minPosX, minPosY));
                }

                // left
                this.ConnectedElements.Add(GetFromCord(data.Elements, minPosX, PosY));

                // left bottom
                if (maxPosY < data.Rows && data.ConnectionType == ConnectionType.Moore)
                {
                    this.ConnectedElements.Add(GetFromCord(data.Elements, minPosX, maxPosY));
                }
            }

            // top
            if (minPosY >= 0)
            {
                this.ConnectedElements.Add(GetFromCord(data.Elements, PosX, minPosY));
            }

            // bottom
            if (maxPosY < data.Rows)
            {
                this.ConnectedElements.Add(GetFromCord(data.Elements, PosX, maxPosY));
            }

            // right side
            if (maxPosX < data.Columns)
            {
                // right top
                if (minPosY >= 0 && data.ConnectionType == ConnectionType.Moore)
                {
                    this.ConnectedElements.Add(GetFromCord(data.Elements, maxPosX, minPosY));
                }

                // right
                this.ConnectedElements.Add(GetFromCord(data.Elements, maxPosX, PosY));

                // right bottom
                if (maxPosY < data.Rows && data.ConnectionType == ConnectionType.Moore)
                {
                    this.ConnectedElements.Add(GetFromCord(data.Elements, maxPosX, maxPosY));
                }
            }
        }

        #endregion Methods
    }
}